//app.js
const CONFIG = require('./config.js');
App({
  onLaunch: function () {
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.request({
          url: "https://wx.huidailang.cn/prod-api/system/weiXinLoginController/decodeOpenId",
          data: {
            code: res.code
          },
          method: "GET",
          header: {
            'content-type': 'application/json',
          },
          success: res => {
            if(res.data.code === "success"){
              let data = res.data.data;
              let loginData = {
                openid : data.openid || '',
                token : 'Bearer ' + data.token || '',
              }
              wx.setStorageSync('loginInfo', JSON.stringify(loginData));
              
              if(!data.token){
                let pages = getCurrentPages();
                let currPage = null;
                if (pages.length) {
                  currPage = pages[pages.length - 1];
                  if(currPage.route === 'pages/register/register'){
                    return;
                  }
                }
                wx.redirectTo({
                  url: '/pages/login/login'
                })
                return;
              }
              this.globalData.loginInfo = loginData;
              if (this.decodeOpenIdCallback) {
                this.decodeOpenIdCallback(loginData)
              }
            }
          },fail: function (error) {
            console.log(error);
          }
        })
      }
    })
	
	// 获取导航高度
	wx.getSystemInfo({
		success: (res) => {
			this.globalData.statusBarHeight = res.statusBarHeight;
			this.globalData.navBarHeight = 44 + res.statusBarHeight;
		}
	})
  
  },
  globalData: {
    hostUrl : 'https://wx.huidailang.cn/',
    userInfo: null,
    loginInfo: null,
    
    statusBarHeight: 0,
    screenHeight: 0
  },
})